package com.ywm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywm.entity.Content;
import com.ywm.mapper.ContentMapper;
import com.ywm.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.StringContent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content> implements ContentService {
    @Autowired
    ContentMapper contentMapper;

    @Override
    public boolean deleteContentById(String contentId) {
        QueryWrapper<Content> wrapper = new QueryWrapper<>();
        wrapper.eq("content_id", contentId);
        int isDelete = contentMapper.delete(wrapper);
        return !(isDelete == 0);
    }

    @Override
    public Map<String,Object> queryContent(int page, int size, String blogId) {
        HashMap<String, Object> map = new HashMap<>();
        IPage<Content> contentPage = new Page<>();
        contentPage.setCurrent(page);
        contentPage.setSize(size);
        QueryWrapper<Content> wrapper = new QueryWrapper<>();
        wrapper.eq("blog_id",blogId);
        IPage<Content> result = contentMapper.selectPage(contentPage, wrapper);
        //当前页
        map.put("currentPage",result.getCurrent());
        //数据
        map.put("data",result.getRecords());
        //总条数
        map.put("totalSize",result.getTotal());
        //总页数
        map.put("pageSize",result.getPages());
        //当前页显示的条数
        map.put("size",result.getSize());
        return map;
    }
}
